package character.sprite.data;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;

import main.Core;

import character.sprite.model.CharacterSprite;

import configuration.Paths;
import data.DataElement;
import data.DataTools;
import data.DataElement.DataElementType;

/**
 * @deprecated
 * @author Richard
 *
 */
public class CharacterSpriteDatabase {

	private static CharacterSpriteDatabase instance;

	private HashMap<String, CharacterSprite> sprites;

	private CharacterSpriteDatabase() {
		sprites = new HashMap<String, CharacterSprite>();
		this.updateFromFiles();
	}

	public static CharacterSpriteDatabase getInstance() {
		if (instance == null) {
			instance = new CharacterSpriteDatabase();
		}
		return instance;
	}

	public void updateFromFiles() {
		String spritesFilePath = Paths.characterDataPath
				+ File.separator
				+ "character_sprites.rpg";
		try {
			File spritesFile = new File(spritesFilePath);
			FileInputStream fis = new FileInputStream(spritesFile);
			ObjectInputStream ois = new ObjectInputStream(fis);
			DataElement rootElement = (DataElement) ois.readObject();
			ArrayList<DataElement> spriteElements = rootElement
					.getAllElementsOfName("Character Sprite");
			for (DataElement de : spriteElements) {
				CharacterSprite cs = new CharacterSprite((String) de
						.getChildElementOfName("Name").getValue(), (String) de
						.getChildElementOfName("Body").getValue(), (String) de
						.getChildElementOfName("Head").getValue(), (String) de
						.getChildElementOfName("Hair").getValue(), (String) de
						.getChildElementOfName("Dress").getValue(), (String) de
						.getChildElementOfName("Face").getValue());
				sprites.put((String) de.getChildElementOfName("Name")
						.getValue(), cs);
			}
		} catch (FileNotFoundException e) {
			Core.getInstance().logMessage(
					this.getClass().getName(),
					"\tSprites file "
							+ spritesFilePath
							+ " could not be found.", e);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public ArrayList<String> getAvailableSprites() {
		return new ArrayList<String>(sprites.keySet());
	}

	public CharacterSprite getSprite(String name) {
		return sprites.get(name);
	}

	public boolean spriteNameExists(String name) {
		return sprites.keySet().contains(name);
	}

	public void addNewSprite(CharacterSprite cs) {
		if (!this.spriteNameExists(cs.getName())) {
			sprites.put(cs.getName(), cs);
		}
	}

	public void deleteSprite(String name) {
		sprites.remove(name);
	}

	public void changeSpriteName(String oldName, String newName) {
		CharacterSprite temp = sprites.get(oldName);
		temp.setName(newName);
		sprites.remove(oldName);
		sprites.put(newName, temp);
	}

	public void setSpriteStyle(String spriteName, String styleType,
			String newStyle) {
		if (styleType.equals("Body")) {
			sprites.get(spriteName).setBody(newStyle);
		} else if (styleType.equals("Hair")) {
			sprites.get(spriteName).setHair(newStyle);
		} else if (styleType.equals("Head")) {
			sprites.get(spriteName).setHead(newStyle);
		} else if (styleType.equals("Face")) {
			sprites.get(spriteName).setFace(newStyle);
		} else if (styleType.equals("Dress")) {
			sprites.get(spriteName).setDress(newStyle);
		}
	}

	public void save() {
		DataElement root = new DataElement("Sprite Database",
				DataElementType.COMPLEX, null);
		for (String s : sprites.keySet()) {
			CharacterSprite cs = sprites.get(s);
			DataElement spriteElement = new DataElement("Character Sprite",
					DataElementType.COMPLEX, null);
			spriteElement.addChildElement(new DataElement("Name",
					DataElementType.STRING, cs.getName()));
			spriteElement.addChildElement(new DataElement("Body",
					DataElementType.STRING, cs.getBody()));
			spriteElement.addChildElement(new DataElement("Head",
					DataElementType.STRING, cs.getHead()));
			spriteElement.addChildElement(new DataElement("Hair",
					DataElementType.STRING, cs.getHair()));
			spriteElement.addChildElement(new DataElement("Face",
					DataElementType.STRING, cs.getFace()));
			spriteElement.addChildElement(new DataElement("Dress",
					DataElementType.STRING, cs.getDress()));
			root.addChildElement(spriteElement);
		}
		try {
			File spritesDir = new File(Paths.characterDataPath
					+ File.separator
					+ "character_sprites.rpg");
			FileOutputStream fos = new FileOutputStream(spritesDir);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(root);
			oos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
